package _interview150;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 71. 简化路径
 */
public class No71 {
    public String simplifyPath(String path) {
        Deque<String> deque = new LinkedList<>();
        for (String s : path.split("/")) {
            if (!s.equals(".") && !s.isEmpty()) {
                if (!s.equals("..")) deque.push(s);
                else if (!deque.isEmpty()) deque.pop();
            }
        }
        StringBuilder ans = new StringBuilder();
        while (!deque.isEmpty()) {
            ans.append("/");
            ans.append(deque.pollLast());
        }
        return ans.isEmpty() ? "/" : ans.toString();
    }
}
